package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.config.VerificationCode;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.model.User;
import com.qf.studentmanager.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
//        response.setContentType("application/json;charset=UTF-8")
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService userService = new UserService();
        Integer result = userService.login(request,username, password);
        RespBean respBean = null;
        ObjectMapper objectMapper = new ObjectMapper();


        //        String vcode = (String) request.getSession().getAttribute("vcode");
        String string = request.getSession().getAttribute("vcode").toString();
        String vcode = request.getParameter("vcode");
        if (vcode == null || !vcode.equalsIgnoreCase(string)){
            respBean = RespBean.error("验证码错误，请重新输入");
            response.getWriter().write(objectMapper.writeValueAsString(respBean));
            return;//如果验证码错了，下面就不要执行了。又或者把下面整段用else包起来，但这样就会判断嵌套。不优雅
        }

        if (result == 1) {
            respBean = RespBean.ok("登录成功");
//            User user = new User();
//            user.setUsername(username);
//            user.setPassword(password);
//            request.getSession().setAttribute("user", user);
        } else if (result == -1) {
            respBean = RespBean.error("用户名或密码错误");

        } else if (result == -2) {
            respBean = RespBean.error("用户被禁用，请联系管理员");
        }


        String s = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(s);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("vcode".equals(action)){
            VerificationCode verificationCode = new VerificationCode();
            BufferedImage image = verificationCode.getImage();
            String text = verificationCode.getText();
            request.getSession().setAttribute("vcode", text); // 在页面显示验证码，同时也为了用户填入验证码点击登录之后，adminservlet可以拿到用户填的和放在session里的进行比较，看是否填错
            VerificationCode.output(image, response.getOutputStream()); //用流的方式传回用text制作成图片的码。
//            return;
        }else {

            request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
//            System.out.println("000");
        }


    }
}
